<?php
/**
*
* @package acp_molten_chat
* @version $Id: a2.1.0-2
* @copyright (c) 2008-2009 Matthew Oltendorf
* @website http://moltx.name/index.php
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License (GPL) Version 2
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

/**
* @package acp
*/
class acp_molten_chat
{
	var $u_action;
	var $new_config;
	
	function main($id, $mode)
	{
		global $db, $user, $auth, $template;
		global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;

		$installed = isset($config['molten_chat_installed']);
		
		if ($installed)
		{
			$user->add_lang('acp/board');

			$action	= request_var('action', '');
			$submit = (isset($_POST['submit'])) ? true : false;

			$form_key = 'acp_board';
			add_form_key($form_key);

			switch ($mode)
			{
				case 'settings':
					$display_vars = array(
						'title'	=> 'MOLTEN_CHAT_SETTINGS',
						'vars'	=> array(
							'legend1'						=> 'MOLTEN_CHAT_SETTINGS_LEGEND',
							'molten_chat_enable'			=> array('lang' => 'MOLTEN_CHAT_ENABLE',			'validate' => 'bool',		'type' => 'radio:yes_no',	'explain' => true),
							'molten_chat_cache_type'		=> array('lang' => 'MOLTEN_CHAT_CACHE_TYPE',		'validate' => 'int:0:1',	'type' => 'select',			'function' => 'build_select',	'params' => array(array('MOLTEN_CHAT_APC', 'MOLTEN_CHAT_SHMOP'), $config['molten_chat_cache_type']),	'explain' => true),
							'molten_chat_messages_reply'	=> array('lang' => 'MOLTEN_CHAT_MESSAGES_REPLY',	'validate' => 'bool',		'type' => 'radio:yes_no',	'explain' => true),
							'molten_chat_messages_topic'	=> array('lang' => 'MOLTEN_CHAT_MESSAGES_TOPIC',	'validate' => 'bool',		'type' => 'radio:yes_no',	'explain' => true)
						)
					);
				break;

				default:
					trigger_error('NO_MODE', E_USER_ERROR);
				break;
			}

			if (isset($display_vars['lang']))
			{
				$user->add_lang($display_vars['lang']);
			}

			$this->new_config = $config;
			$cfg_array = (isset($_REQUEST['config'])) ? utf8_normalize_nfc(request_var('config', array('' => ''), true)) : $this->new_config;
			$error = array();

			// We validate the complete config if wished.
			validate_config_vars($display_vars['vars'], $cfg_array, $error);

			if ($submit && !check_form_key($form_key))
			{
				$error[] = $user->lang['FORM_INVALID'];
			}
			// Do not write values if there is an error.
			if (sizeof($error))
			{
				$submit = false;
			}

			// We go through the display_vars to make sure no one is trying to set variables he/she is not allowed to...
			foreach ($display_vars['vars'] as $config_name => $null)
			{
				if (!isset($cfg_array[$config_name]) || strpos($config_name, 'legend') !== false)
				{
					continue;
				}

				$this->new_config[$config_name] = $config_value = $cfg_array[$config_name];

				if ($submit)
				{
					set_config($config_name, $config_value);
				}
			}

			if ($submit)
			{
				add_log('admin', 'LOG_CONFIG_MOLTEN_CHAT_' . strtoupper($mode));

				trigger_error($user->lang['CONFIG_UPDATED'] . adm_back_link($this->u_action));
			}

			$this->tpl_name = 'acp_board';
			$this->page_title = $display_vars['title'];

			$template->assign_vars(array(
				'L_TITLE'			=> $user->lang[$display_vars['title']],
				'L_TITLE_EXPLAIN'	=> $user->lang[$display_vars['title'] . '_EXPLAIN'],

				'S_ERROR'			=> (sizeof($error)) ? true : false,
				'ERROR_MSG'			=> implode('<br />', $error),

				'U_ACTION'			=> $this->u_action)
			);

			// Output relevant page.
			foreach ($display_vars['vars'] as $config_key => $vars)
			{
				if (!is_array($vars) && strpos($config_key, 'legend') === false)
				{
					continue;
				}

				if (strpos($config_key, 'legend') !== false)
				{
					$template->assign_block_vars('options', array(
						'S_LEGEND'		=> true,
						'LEGEND'		=> (isset($user->lang[$vars])) ? $user->lang[$vars] : $vars)
					);

					continue;
				}

				$type = explode(':', $vars['type']);

				$l_explain = '';
				if ($vars['explain'] && isset($vars['lang_explain']))
				{
					$l_explain = (isset($user->lang[$vars['lang_explain']])) ? $user->lang[$vars['lang_explain']] : $vars['lang_explain'];
				}
				else if ($vars['explain'])
				{
					$l_explain = (isset($user->lang[$vars['lang'] . '_EXPLAIN'])) ? $user->lang[$vars['lang'] . '_EXPLAIN'] : '';
				}

				$content = build_cfg_template($type, $config_key, $this->new_config, $config_key, $vars);

				if (empty($content))
				{
					continue;
				}

				$template->assign_block_vars('options', array(
					'KEY'			=> $config_key,
					'TITLE'			=> (isset($user->lang[$vars['lang']])) ? $user->lang[$vars['lang']] : $vars['lang'],
					'S_EXPLAIN'		=> $vars['explain'],
					'TITLE_EXPLAIN'	=> $l_explain,
					'CONTENT'		=> $content,
					)
				);

				unset($display_vars['vars'][$config_key]);
			}

			return;
		}

		// Not installed.
		trigger_error($user->lang['MOLTEN_CHAT_ERROR_NOT_INSTALLED'] . adm_back_link($this->u_action), E_USER_WARNING);
		//'MoltX QuickChat 2.1 for phpBB 3 has not been properly installed. Please refer to the MODX file, and complete the installation.'
	}
}

?>